﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YArchitech.Plumbing.Model
{
	public class RectangleRange
	{
		public RectangleRange(XYZ _p1, XYZ _p2, XYZ _p3, XYZ _p4)
		{
			this.p1 = _p1;
			this.xyzList.Add(this.p1);
			this.p2 = _p2;
			this.xyzList.Add(this.p2);
			this.p3 = _p3;
			this.xyzList.Add(this.p3);
			this.p4 = _p4;
			this.xyzList.Add(this.p4);
			this.l1 = Line.CreateBound(this.p1, this.p2);
			this.lineList.Add(this.l1);
			this.l2 = Line.CreateBound(this.p2, this.p3);
			this.lineList.Add(this.l2);
			this.l3 = Line.CreateBound(this.p3, this.p4);
			this.lineList.Add(this.l3);
			this.l4 = Line.CreateBound(this.p4, this.p1);
			this.lineList.Add(this.l4);
			this.setMaxMinValue();
			this.center = new XYZ((this.minx + this.maxx) / 2.0, (this.miny + this.maxy) / 2.0, 0.0);
		}

		private void setMaxMinValue()
		{
			this.maxx = (this.minx = this.p1.X);
			this.miny = (this.maxy = this.p1.Y);
			if (this.p1.X > this.maxx)
			{
				this.maxx = this.p1.X;
			}
			else if (this.p1.X < this.minx)
			{
				this.minx = this.p1.X;
			}
			if (this.p2.X > this.maxx)
			{
				this.maxx = this.p2.X;
			}
			else if (this.p2.X < this.minx)
			{
				this.minx = this.p2.X;
			}
			if (this.p3.X > this.maxx)
			{
				this.maxx = this.p3.X;
			}
			else if (this.p3.X < this.minx)
			{
				this.minx = this.p3.X;
			}
			if (this.p4.X > this.maxx)
			{
				this.maxx = this.p4.X;
			}
			else if (this.p4.X < this.minx)
			{
				this.minx = this.p4.X;
			}
			if (this.p1.Y > this.maxy)
			{
				this.maxy = this.p1.Y;
			}
			else if (this.p1.Y < this.miny)
			{
				this.miny = this.p1.Y;
			}
			if (this.p2.Y > this.maxy)
			{
				this.maxy = this.p2.Y;
			}
			else if (this.p2.Y < this.miny)
			{
				this.miny = this.p2.Y;
			}
			if (this.p3.Y > this.maxy)
			{
				this.maxy = this.p3.Y;
			}
			else if (this.p3.Y < this.miny)
			{
				this.miny = this.p3.Y;
			}
			if (this.p4.Y > this.maxy)
			{
				this.maxy = this.p4.Y;
				return;
			}
			if (this.p4.Y < this.miny)
			{
				this.miny = this.p4.Y;
			}
		}

		public bool InsidePolygon(XYZ p)
		{
			List<XYZ> list = new List<XYZ>();
			list.Add(this.p1);
			list.Add(this.p2);
			list.Add(this.p3);
			list.Add(this.p4);
			if (list.Count <= 0)
			{
				return false;
			}
			int num = 0;
			XYZ xyz = list[0];
			int count = list.Count;
			for (int i = 1; i <= count; i++)
			{
				XYZ xyz2 = list[i % count];
				if (xyz.Y != xyz2.Y && p.Y > Math.Min(xyz.Y, xyz2.Y) && p.Y <= Math.Max(xyz.Y, xyz2.Y) && p.X <= Math.Max(xyz.X, xyz2.X))
				{
					if (xyz.X == xyz2.X)
					{
						num++;
					}
					else
					{
						double num2 = (p.Y - xyz.Y) * (xyz2.X - xyz.X) / (xyz2.Y - xyz.Y) + xyz.X;
						if (p.X <= num2)
						{
							num++;
						}
					}
				}
				xyz = xyz2;
			}
			return num % 2 != 0;
		}

		public double maxx = double.NegativeInfinity;

		public double maxy = double.NegativeInfinity;

		public double minx = double.PositiveInfinity;

		public double miny = double.PositiveInfinity;

		public XYZ p1;

		public XYZ p2;

		public XYZ p3;

		public XYZ p4;

		public List<XYZ> xyzList = new List<XYZ>();

		public Line l1;

		public Line l2;

		public Line l3;

		public Line l4;

		public List<Line> lineList = new List<Line>();

		public XYZ center;
	}
}
